//
//  PositionDistanceView.swift
//  AirTravel
//
//  Created by apple on 2021/7/15.
//

import UIKit

protocol PositionDistanceDelegate:NSObjectProtocol {
    func positionDistanceClick(zoneCode:Dictionary<String,Any>,trainStation:Dictionary<String,Any>,airport:Dictionary<String,Any>,metroStation:Dictionary<String,Any>,viewSpot:Dictionary<String,Any>,hotelLocationType:String,hotel:String,areaId:String)
}

class PositionDistanceView: UIView {
    var contentView:UIView!
    weak var delegate: PositionDistanceDelegate?
    @IBOutlet weak var rightTab: UITableView!
    @IBOutlet weak var leftTab: UITableView!
    
    /// 地铁线路
    @IBOutlet weak var subwayLineTab: UITableView!
    /// 地铁
    @IBOutlet weak var subwayTab: UITableView!
    
    /// 完成
    @IBOutlet weak var completeBtn: UIButton!
    /// 清空
    @IBOutlet weak var emptyBtn: UIButton!
    var dataSouce : Array<Any>?
    var number = 0
    var number1 = -1
    var count = 0
    var count1 = -1
    
    /// 酒店筛选位置类型 1机场 2火车站 3地铁 4酒店 5商圈 6地标 7行政区
    var hotelLocationType = ""
    /// 商圈
    var zoneCodeDic : Dictionary<String,Any>?
    /// 火车站
    var trainStationDic : Dictionary<String,Any>?
    /// 机场
    var airportDic : Dictionary<String,Any>?
    /// 地铁站
    var metroStationDic : Dictionary<String,Any>?
    /// 景点
    var viewSpotDic : Dictionary<String,Any>?
    /// 酒店关键词
    var hotel = ""
    /// 行政区
    var areaId = ""
    
    
    let identifyIR = "IntelligentRankingCell"
    
    
    // 初始化时将xib中的view添加进来
    override init(frame: CGRect) {
        super.init(frame: frame)
        contentView = loadViewFromNib()
        contentView.frame = self.bounds
        contentView.backgroundColor = UIColor.white
        self.insertSubview(contentView, at: 0)
        
        self.emptyBtn.layer.cornerRadius = 6
        self.emptyBtn.layer.borderWidth = 1
        self.emptyBtn.layer.borderColor = UIColor.init(r: 212, g: 212, b: 212).cgColor
        self.emptyBtn.addTarget(self, action: #selector(empty), for: .touchUpInside)
        
        self.completeBtn.layer.cornerRadius = 6
        self.completeBtn.layer.masksToBounds = true
        self.completeBtn.addTarget(self, action: #selector(complete), for: .touchUpInside)
        
        self.leftTab.dataSource = self
        self.leftTab.delegate = self
        self.leftTab.tag = 0
        self.leftTab.rowHeight = 50
        self.leftTab.register(UINib.init(nibName: "IntelligentRankingCell", bundle: nil), forCellReuseIdentifier: identifyIR)
        self.leftTab.tableFooterView = UIView.init()
        
        self.rightTab.dataSource = self
        self.rightTab.delegate = self
        self.rightTab.tag = 1
        self.rightTab.rowHeight = 50
        self.rightTab.register(UINib.init(nibName: "IntelligentRankingCell", bundle: nil), forCellReuseIdentifier: identifyIR)
        self.rightTab.tableFooterView = UIView.init()
        
        self.subwayLineTab.dataSource = self
        self.subwayLineTab.delegate = self
        self.subwayLineTab.tag = 2
        self.subwayLineTab.rowHeight = 50
        self.subwayLineTab.register(UINib.init(nibName: "IntelligentRankingCell", bundle: nil), forCellReuseIdentifier: identifyIR)
        self.subwayLineTab.tableFooterView = UIView.init()
        
        self.subwayTab.dataSource = self
        self.subwayTab.delegate = self
        self.subwayTab.tag = 3
        self.subwayTab.rowHeight = 50
        self.subwayTab.register(UINib.init(nibName: "IntelligentRankingCell", bundle: nil), forCellReuseIdentifier: identifyIR)
        self.subwayTab.tableFooterView = UIView.init()
    }
    
    // 初始化时将xib中的view添加进来
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    // 加载xib
    func loadViewFromNib() -> UIView {
        let className = type(of: self)
        let bundle = Bundle(for: className)
        let name = NSStringFromClass(className).components(separatedBy: ".").last
        let nib = UINib(nibName: name!, bundle: bundle)
        let view = nib.instantiate(withOwner: self, options: nil).first as! UIView
        view.backgroundColor = UIColor.clear
        return view
    }
    
    @objc func empty() {
        self.number = 0
        self.number1 = -1
        self.count = 0
        self.count1 = -1
        self.leftTab.reloadData()
        self.rightTab.reloadData()
        self.subwayLineTab.reloadData()
        self.subwayTab.reloadData()
        /// 酒店筛选位置类型 1机场 2火车站 3地铁 4酒店 5商圈 6地标 7行政区
        self.hotelLocationType = ""
        /// 商圈
        self.zoneCodeDic = nil
        /// 火车站
        self.trainStationDic = nil
        /// 机场
        self.airportDic = nil
        /// 地铁站
        self.metroStationDic = nil
        /// 景点
        self.viewSpotDic = nil
        /// 酒店关键词
        self.hotel = ""
        /// 行政区
        self.areaId = ""
        self.delegate?.positionDistanceClick(zoneCode:[:], trainStation: [:], airport:[:], metroStation:[:], viewSpot:[:], hotelLocationType: self.hotelLocationType, hotel: self.hotel, areaId: self.areaId)
    }
    
    @objc func complete() {
        GKCover.hideWithoutAnimation()
        self.delegate?.positionDistanceClick(zoneCode:self.zoneCodeDic ?? [:], trainStation: self.trainStationDic ?? [:], airport:self.airportDic ?? [:], metroStation:self.metroStationDic ?? [:], viewSpot:self.viewSpotDic ?? [:], hotelLocationType: self.hotelLocationType, hotel: self.hotel, areaId: self.areaId)
    }
    
    func setUpData(array:Array<Any>) {
        self.dataSouce = array
        self.leftTab.reloadData()
        self.rightTab.reloadData()
        self.subwayLineTab.reloadData()
        self.subwayTab.reloadData()
    }
}

extension PositionDistanceView:UITableViewDataSource,UITableViewDelegate{
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        guard let array = self.dataSouce else { return 0 }
        let dic = array[self.number] as? Dictionary<String,Any>
        switch tableView.tag {
        case 0:
            return self.dataSouce?.count ?? 0
        case 1,2:
            let infolist = dic?["infolist"] as? Array<Any>
            return infolist?.count ?? 0
        case 3:
            let infolist = dic?["infolist"] as? Array<Any>
            let keyword = "\(dic?["keyword"] ?? "")"
            if keyword == "地铁站" {
                let dict = infolist?[self.count] as? Dictionary<String,Any>
                let infolistArray = dict?["infolist"] as? Array<Any>
                return infolistArray?.count ?? 0
            }else{
                return 0
            }
        default:
            return 0
        }
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: identifyIR) as! IntelligentRankingCell
        cell.selectionStyle = .none
        guard let array = self.dataSouce else { return cell }
        let dic = array[self.number] as? Dictionary<String,Any>
        let infolist = dic?["infolist"] as? Array<Any>
        let keyword = "\(dic?["keyword"] ?? "")"
        switch tableView.tag {
        case 0:
            let dic0 = array[indexPath.row] as? Dictionary<String,Any>
            cell.contentLab.text = "\(dic0?["keyword"] ?? "")"
            if self.number == indexPath.row {
                cell.contentLab.textColor = Orange_COLOR
            }else{
                cell.contentLab.textColor = .black
            }
        case 1:
            if let array1 = infolist {
                let dic1 = array1[indexPath.row] as? Dictionary<String,Any>
                cell.contentLab.text = "\(dic1?["InfoName"] ?? "")"
                if self.number1 == indexPath.row {
                    cell.contentLab.textColor = Orange_COLOR
                }else{
                    cell.contentLab.textColor = .black
                }
            }
        case 2:
            if keyword == "地铁站" {
                if let array1 = infolist {
                    let dict = array1[indexPath.row] as? Dictionary<String,Any>
                    cell.contentLab.text = "\(dict?["line"] ?? "")"
                    if self.count == indexPath.row {
                        cell.contentLab.textColor = Orange_COLOR
                    }else{
                        cell.contentLab.textColor = .black
                    }
                }
            }
        default:
            if keyword == "地铁站" {
                if let array1 = infolist {
                    let dict = array1[self.count] as? Dictionary<String,Any>
                    let infolistArray = dict?["infolist"] as? Array<Any>
                    let dict2 = infolistArray?[indexPath.row] as? Dictionary<String,Any>
                    cell.contentLab.text = "\(dict2?["InfoName"] ?? "")"
                    if self.count1 == indexPath.row {
                        cell.contentLab.textColor = Orange_COLOR
                    }else{
                        cell.contentLab.textColor = .black
                    }
                }
            }
        }
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){
        guard let array = self.dataSouce else { return }
        switch tableView.tag {
        case 0:
            let dic = array[indexPath.row] as? Dictionary<String,Any>
            let keyword = "\(dic?["keyword"] ?? "")"
            self.number = indexPath.row
            if keyword == "地铁站" {
                self.subwayTab.isHidden = false
                self.subwayLineTab.isHidden = false
                
            }else{
                self.subwayTab.isHidden = true
                self.subwayLineTab.isHidden = true
            }
            switch keyword {
            case "地铁站":
                self.hotelLocationType = "3"
            case "热搜酒店":
                self.hotelLocationType = "4"
            case "行政区":
                self.hotelLocationType = "7"
            case "飞机场":
                self.hotelLocationType = "1"
            case "火车站":
                self.hotelLocationType = "2"
            case "景点/景区":
                self.hotelLocationType = "6"
            case "商圈":
                self.hotelLocationType = "5"
            default:
                break
            }
        case 1:
            self.number1 = indexPath.row
            let dic = array[self.number] as? Dictionary<String,Any>
            let infolist = dic?["infolist"] as? Array<Any>
            let dic1 = infolist?[indexPath.row] as? Dictionary<String,Any>
            switch self.hotelLocationType {
            case "1":
                self.airportDic = dic1
            case "2":
                self.trainStationDic = dic1
            case "4":
                let InfoName = "\(dic1?["InfoName"] ?? "")"
                self.hotel = InfoName
            case "5":
                self.zoneCodeDic = dic1
            case "6":
                self.viewSpotDic = dic1
            case "7":
                let InfoId = "\(dic1?["InfoId"] ?? "")"
                self.areaId = InfoId
            default:
                break
            }
        case 2:
            self.count = indexPath.row
        case 3:
            self.count1 = indexPath.row
            let dic = array[self.number] as? Dictionary<String,Any>
            let infolist = dic?["infolist"] as? Array<Any>
            if let array1 = infolist {
                let dict = array1[self.count] as? Dictionary<String,Any>
                let infolistArray = dict?["infolist"] as? Array<Any>
                let dict2 = infolistArray?[indexPath.row] as? Dictionary<String,Any>
                self.metroStationDic = dict2
            }
        default:
            break
        }
        self.leftTab.reloadData()
        self.rightTab.reloadData()
        self.subwayLineTab.reloadData()
        self.subwayTab.reloadData()
    }
    
}
